package com.wzy.level.comment.commentDaoImpl;

import com.wzy.Jdbc封装.dao.NewsDao;
import com.wzy.Jdbc封装.pojo.News_comment;
import com.wzy.level.byPage.byPageDto.ByPage;
import com.wzy.level.comment.commentDao.commentDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class commentDaoImpl implements commentDao {
    @Override
    public List<News_comment> selectAll() {
        ResultSet resultSet =null;
        ArrayList<News_comment> list = new ArrayList<>();
        try {
            String  sql = "SELECT * FROM `news_comment`";
            Object[] obj={};
            resultSet = NewsDao.selectById1(sql, obj);
            while(resultSet.next()) {
                News_comment news_comment = new News_comment();
                int id1 = resultSet.getInt("id");
                int newsId = resultSet.getInt(2);
                String content = resultSet.getString(3);
                String author = resultSet.getString(4);
                String ip = resultSet.getString(5);
                java.util.Date date = resultSet.getDate(6);
                news_comment.setId(id1);
                news_comment.setNewsId(newsId);
                news_comment.setContent(content);
                news_comment.setAuthor(author);
                news_comment.setIp(ip);
                news_comment.setDate(date);
                list.add(news_comment);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            NewsDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return list;
    }

    @Override
    public News_comment selectById(int id) {
        ResultSet resultSet =null;
        News_comment news_comment = new News_comment();
        try {
            String  sql = "SELECT * FROM `news_comment` WHERE id=?";
            Object[] obj={id};
            resultSet = NewsDao.selectById11(sql, obj);
            while(resultSet.next()) {
                int id1 = resultSet.getInt("id");
                int newsId = resultSet.getInt(2);
                String content = resultSet.getString(3);
                String author = resultSet.getString(4);
                String ip = resultSet.getString(5);
                java.util.Date date = resultSet.getDate(6);
                news_comment.setId(id1);
                news_comment.setNewsId(newsId);
                news_comment.setContent(content);
                news_comment.setAuthor(author);
                news_comment.setIp(ip);
                news_comment.setDate(date);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            NewsDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return news_comment;
    }

    @Override
    public List<News_comment> selectByPage(ByPage byPage) {
        ResultSet resultSet =null;
        ArrayList<News_comment> list = new ArrayList<>();
        News_comment news_comment = new News_comment();
        try {
            String  sql = "SELECT * FROM `news_comment` WHERE LIMIT ?,?";
            Object[] obj={byPage};
            resultSet = NewsDao.selectByPage(sql, obj);
            while(resultSet.next()) {
                int id1 = resultSet.getInt("id");
                int newsId = resultSet.getInt(2);
                String content = resultSet.getString(3);
                String author = resultSet.getString(4);
                String ip = resultSet.getString(5);
                java.util.Date date = resultSet.getDate(6);
                news_comment.setId(id1);
                news_comment.setNewsId(newsId);
                news_comment.setContent(content);
                news_comment.setAuthor(author);
                news_comment.setIp(ip);
                news_comment.setDate(date);
                list.add(news_comment);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            NewsDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return list;
    }

    @Override
    public int insert(News_comment comment) {
        int rows = 0;
        String sql = "INSERT INTO `newsdb`.`news_comment` (`id`, `newsId`, `content`, `author`, `ip`, `createDate`) VALUES (?, ?, ?, ?, ?, ?)";
        Object[] obj = {comment};
        rows = NewsDao.insert1(sql, obj);
        return rows;
    }

    @Override
    public int update(News_comment comment) {
        int rows = 0;
        String sql = "UPDATE `news_comment` SET newsId=?, content=? ,author=?, ip=? ,createDate=? WHERE id=?";
        Object[] obj = {comment};
        rows = NewsDao.update11(sql, obj);
        return rows;
    }

    @Override
    public int deleteById(News_comment comment) {
        return 0;
    }

    @Override
    public int deleteByName(News_comment comment) {
        return 0;
    }
}
